<!DOCTYPE html>
<html lang="" xml:lang="">
<head>

  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <title>第 1 章 准备工作 | 极客R：数据分析之道</title>
  <meta name="description" content="一本极简 R 入门图书" />
  <meta name="generator" content="bookdown 0.19 and GitBook 2.6.7" />

  <meta property="og:title" content="第 1 章 准备工作 | 极客R：数据分析之道" />
  <meta property="og:type" content="book" />
  
  <meta property="og:image" content="cover.png" />
  <meta property="og:description" content="一本极简 R 入门图书" />
  <meta name="github-repo" content="ShixiangWang/geek-r-tutorial" />

  <meta name="twitter:card" content="summary" />
  <meta name="twitter:title" content="第 1 章 准备工作 | 极客R：数据分析之道" />
  
  <meta name="twitter:description" content="一本极简 R 入门图书" />
  <meta name="twitter:image" content="cover.png" />

<meta name="author" content="王诗翔, 生信技能树" />



  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  
  
<link rel="prev" href="author.html"/>
<link rel="next" href="base.html"/>
<script src="libs/jquery/jquery.min.js"></script>
<link href="libs/gitbook/css/style.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-table.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-fontsettings.css" rel="stylesheet" />
<link href="libs/gitbook/css/plugin-clipboard.css" rel="stylesheet" />











<style type="text/css">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
  { position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
  { content: attr(data-line-number);
    position: relative; left: -1em; text-align: right; vertical-align: baseline;
    border: none; pointer-events: all; display: inline-block;
    -webkit-touch-callout: none; -webkit-user-select: none;
    -khtml-user-select: none; -moz-user-select: none;
    -ms-user-select: none; user-select: none;
    padding: 0 4px; width: 4em;
    color: #aaaaaa;
  }
pre.numberSource { margin-left: 3em; border-left: 1px solid #aaaaaa;  padding-left: 4px; }
div.sourceCode
  {  }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ff0000; font-weight: bold; } /* Alert */
code span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code span.at { color: #7d9029; } /* Attribute */
code span.bn { color: #40a070; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code span.ch { color: #4070a0; } /* Char */
code span.cn { color: #880000; } /* Constant */
code span.co { color: #60a0b0; font-style: italic; } /* Comment */
code span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code span.do { color: #ba2121; font-style: italic; } /* Documentation */
code span.dt { color: #902000; } /* DataType */
code span.dv { color: #40a070; } /* DecVal */
code span.er { color: #ff0000; font-weight: bold; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #40a070; } /* Float */
code span.fu { color: #06287e; } /* Function */
code span.im { } /* Import */
code span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
code span.kw { color: #007020; font-weight: bold; } /* Keyword */
code span.op { color: #666666; } /* Operator */
code span.ot { color: #007020; } /* Other */
code span.pp { color: #bc7a00; } /* Preprocessor */
code span.sc { color: #4070a0; } /* SpecialChar */
code span.ss { color: #bb6688; } /* SpecialString */
code span.st { color: #4070a0; } /* String */
code span.va { color: #19177c; } /* Variable */
code span.vs { color: #4070a0; } /* VerbatimString */
code span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
</style>

<link rel="stylesheet" href="css/style.css" type="text/css" />
</head>

<body>



  <div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">

    <div class="book-summary">
      <nav role="navigation">

<ul class="summary">
<li><a href="./">数据分析之道</a></li>

<li class="divider"></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i>前言</a><ul>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#内容简介"><i class="fa fa-check"></i>内容简介</a></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#许可"><i class="fa fa-check"></i>许可</a></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#建议与反馈"><i class="fa fa-check"></i>建议与反馈</a></li>
<li class="chapter" data-level="" data-path="index.html"><a href="index.html#致谢"><i class="fa fa-check"></i>致谢</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="author.html"><a href="author.html"><i class="fa fa-check"></i>作者简介</a></li>
<li class="chapter" data-level="1" data-path="prepare.html"><a href="prepare.html"><i class="fa fa-check"></i><b>1</b> 准备工作</a><ul>
<li class="chapter" data-level="1.1" data-path="prepare.html"><a href="prepare.html#r-的下载和安装"><i class="fa fa-check"></i><b>1.1</b> R 的下载和安装</a></li>
<li class="chapter" data-level="1.2" data-path="prepare.html"><a href="prepare.html#rstudio-的下载和安装"><i class="fa fa-check"></i><b>1.2</b> RStudio 的下载和安装</a></li>
<li class="chapter" data-level="1.3" data-path="prepare.html"><a href="prepare.html#配置可选"><i class="fa fa-check"></i><b>1.3</b> 配置（可选）</a></li>
<li class="chapter" data-level="1.4" data-path="prepare.html"><a href="prepare.html#常见问题与方案"><i class="fa fa-check"></i><b>1.4</b> 常见问题与方案</a><ul>
<li class="chapter" data-level="1.4.1" data-path="prepare.html"><a href="prepare.html#r-在-linux-系统下的安装"><i class="fa fa-check"></i><b>1.4.1</b> R 在 Linux 系统下的安装</a></li>
<li class="chapter" data-level="1.4.2" data-path="prepare.html"><a href="prepare.html#rtools-安装"><i class="fa fa-check"></i><b>1.4.2</b> Rtools 安装</a></li>
<li class="chapter" data-level="1.4.3" data-path="prepare.html"><a href="prepare.html#rstudio-server-安装"><i class="fa fa-check"></i><b>1.4.3</b> RStudio Server 安装</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="2" data-path="base.html"><a href="base.html"><i class="fa fa-check"></i><b>2</b> 基础语法</a><ul>
<li class="chapter" data-level="2.1" data-path="base.html"><a href="base.html#基本数据结构"><i class="fa fa-check"></i><b>2.1</b> 基本数据结构</a><ul>
<li class="chapter" data-level="2.1.1" data-path="base.html"><a href="base.html#向量"><i class="fa fa-check"></i><b>2.1.1</b> 向量</a></li>
<li class="chapter" data-level="2.1.2" data-path="base.html"><a href="base.html#数组与矩阵"><i class="fa fa-check"></i><b>2.1.2</b> 数组与矩阵</a></li>
<li class="chapter" data-level="2.1.3" data-path="base.html"><a href="base.html#数据框"><i class="fa fa-check"></i><b>2.1.3</b> 数据框</a></li>
<li class="chapter" data-level="2.1.4" data-path="base.html"><a href="base.html#列表"><i class="fa fa-check"></i><b>2.1.4</b> 列表</a></li>
</ul></li>
<li class="chapter" data-level="2.2" data-path="base.html"><a href="base.html#控制结构"><i class="fa fa-check"></i><b>2.2</b> 控制结构</a><ul>
<li class="chapter" data-level="2.2.1" data-path="base.html"><a href="base.html#条件控制"><i class="fa fa-check"></i><b>2.2.1</b> 条件控制</a></li>
<li class="chapter" data-level="2.2.2" data-path="base.html"><a href="base.html#循环控制"><i class="fa fa-check"></i><b>2.2.2</b> 循环控制</a></li>
</ul></li>
<li class="chapter" data-level="2.3" data-path="base.html"><a href="base.html#函数与函数式编程"><i class="fa fa-check"></i><b>2.3</b> 函数与函数式编程</a><ul>
<li class="chapter" data-level="2.3.1" data-path="base.html"><a href="base.html#创建和使用函数"><i class="fa fa-check"></i><b>2.3.1</b> 创建和使用函数</a></li>
<li class="chapter" data-level="2.3.2" data-path="base.html"><a href="base.html#作用域"><i class="fa fa-check"></i><b>2.3.2</b> 作用域</a></li>
<li class="chapter" data-level="2.3.3" data-path="base.html"><a href="base.html#任意参数"><i class="fa fa-check"></i><b>2.3.3</b> 任意参数</a></li>
<li class="chapter" data-level="2.3.4" data-path="base.html"><a href="base.html#函数式编程"><i class="fa fa-check"></i><b>2.3.4</b> 函数式编程</a></li>
</ul></li>
<li class="chapter" data-level="2.4" data-path="base.html"><a href="base.html#三方包的安装与加载"><i class="fa fa-check"></i><b>2.4</b> 三方包的安装与加载</a><ul>
<li class="chapter" data-level="2.4.1" data-path="base.html"><a href="base.html#cran"><i class="fa fa-check"></i><b>2.4.1</b> CRAN</a></li>
<li class="chapter" data-level="2.4.2" data-path="base.html"><a href="base.html#bioconductor"><i class="fa fa-check"></i><b>2.4.2</b> Bioconductor</a></li>
<li class="chapter" data-level="2.4.3" data-path="base.html"><a href="base.html#github-等-git-库"><i class="fa fa-check"></i><b>2.4.3</b> GitHub 等 Git 库</a></li>
<li class="chapter" data-level="2.4.4" data-path="base.html"><a href="base.html#包使用"><i class="fa fa-check"></i><b>2.4.4</b> 包使用</a></li>
</ul></li>
<li class="chapter" data-level="2.5" data-path="base.html"><a href="base.html#编程实战roc-曲线计算与绘制"><i class="fa fa-check"></i><b>2.5</b> 编程实战：ROC 曲线计算与绘制</a><ul>
<li class="chapter" data-level="2.5.1" data-path="base.html"><a href="base.html#背景与目标"><i class="fa fa-check"></i><b>2.5.1</b> 背景与目标</a></li>
<li class="chapter" data-level="2.5.2" data-path="base.html"><a href="base.html#代码实现"><i class="fa fa-check"></i><b>2.5.2</b> 代码实现</a></li>
<li class="chapter" data-level="2.5.3" data-path="base.html"><a href="base.html#代码讲解"><i class="fa fa-check"></i><b>2.5.3</b> 代码讲解</a></li>
</ul></li>
<li class="chapter" data-level="2.6" data-path="base.html"><a href="base.html#常见问题与方案-1"><i class="fa fa-check"></i><b>2.6</b> 常见问题与方案</a><ul>
<li class="chapter" data-level="2.6.1" data-path="base.html"><a href="base.html#复数表示"><i class="fa fa-check"></i><b>2.6.1</b> 复数表示</a></li>
<li class="chapter" data-level="2.6.2" data-path="base.html"><a href="base.html#与---的区别"><i class="fa fa-check"></i><b>2.6.2</b> = 与 &lt;- 的区别</a></li>
<li class="chapter" data-level="2.6.3" data-path="base.html"><a href="base.html#显式使用包函数时-与-的区别"><i class="fa fa-check"></i><b>2.6.3</b> 显式使用包函数时 :: 与 ::: 的区别</a></li>
<li class="chapter" data-level="2.6.4" data-path="base.html"><a href="base.html#因子重构"><i class="fa fa-check"></i><b>2.6.4</b> 因子重构</a></li>
<li class="chapter" data-level="2.6.5" data-path="base.html"><a href="base.html#理解-r-计算"><i class="fa fa-check"></i><b>2.6.5</b> 理解 R 计算</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="3" data-path="import.html"><a href="import.html"><i class="fa fa-check"></i><b>3</b> 数据导入</a><ul>
<li class="chapter" data-level="3.1" data-path="import.html"><a href="import.html#符号分隔文件"><i class="fa fa-check"></i><b>3.1</b> 符号分隔文件</a><ul>
<li class="chapter" data-level="3.1.1" data-path="import.html"><a href="import.html#csv"><i class="fa fa-check"></i><b>3.1.1</b> CSV</a></li>
<li class="chapter" data-level="3.1.2" data-path="import.html"><a href="import.html#tsv-与其他-csv-变体"><i class="fa fa-check"></i><b>3.1.2</b> TSV 与其他 CSV 变体</a></li>
</ul></li>
<li class="chapter" data-level="3.2" data-path="import.html"><a href="import.html#excel"><i class="fa fa-check"></i><b>3.2</b> Excel</a></li>
<li class="chapter" data-level="3.3" data-path="import.html"><a href="import.html#json"><i class="fa fa-check"></i><b>3.3</b> JSON</a></li>
<li class="chapter" data-level="3.4" data-path="import.html"><a href="import.html#r-数据文件"><i class="fa fa-check"></i><b>3.4</b> R 数据文件</a><ul>
<li class="chapter" data-level="3.4.1" data-path="import.html"><a href="import.html#rdata"><i class="fa fa-check"></i><b>3.4.1</b> RData</a></li>
<li class="chapter" data-level="3.4.2" data-path="import.html"><a href="import.html#rds"><i class="fa fa-check"></i><b>3.4.2</b> RDS</a></li>
</ul></li>
<li class="chapter" data-level="3.5" data-path="import.html"><a href="import.html#常见问题与方案-2"><i class="fa fa-check"></i><b>3.5</b> 常见问题与方案</a><ul>
<li class="chapter" data-level="3.5.1" data-path="import.html"><a href="import.html#通过键盘和剪贴板载入数据"><i class="fa fa-check"></i><b>3.5.1</b> 通过键盘和剪贴板载入数据</a></li>
<li class="chapter" data-level="3.5.2" data-path="import.html"><a href="import.html#逐行读取数据"><i class="fa fa-check"></i><b>3.5.2</b> 逐行读取数据</a></li>
<li class="chapter" data-level="3.5.3" data-path="import.html"><a href="import.html#读取等宽格式数据"><i class="fa fa-check"></i><b>3.5.3</b> 读取等宽格式数据</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="4" data-path="clean.html"><a href="clean.html"><i class="fa fa-check"></i><b>4</b> 数据清洗</a><ul>
<li class="chapter" data-level="4.1" data-path="clean.html"><a href="clean.html#向量-1"><i class="fa fa-check"></i><b>4.1</b> 向量</a></li>
<li class="chapter" data-level="4.2" data-path="clean.html"><a href="clean.html#单个数据集"><i class="fa fa-check"></i><b>4.2</b> 单个数据集</a><ul>
<li class="chapter" data-level="4.2.1" data-path="clean.html"><a href="clean.html#选择列"><i class="fa fa-check"></i><b>4.2.1</b> 选择列</a></li>
<li class="chapter" data-level="4.2.2" data-path="clean.html"><a href="clean.html#过滤行"><i class="fa fa-check"></i><b>4.2.2</b> 过滤行</a></li>
<li class="chapter" data-level="4.2.3" data-path="clean.html"><a href="clean.html#新增列"><i class="fa fa-check"></i><b>4.2.3</b> 新增列</a></li>
<li class="chapter" data-level="4.2.4" data-path="clean.html"><a href="clean.html#聚合运算"><i class="fa fa-check"></i><b>4.2.4</b> 聚合运算</a></li>
<li class="chapter" data-level="4.2.5" data-path="clean.html"><a href="clean.html#分组计算"><i class="fa fa-check"></i><b>4.2.5</b> 分组计算</a></li>
<li class="chapter" data-level="4.2.6" data-path="clean.html"><a href="clean.html#缺失值填充"><i class="fa fa-check"></i><b>4.2.6</b> 缺失值填充</a></li>
</ul></li>
<li class="chapter" data-level="4.3" data-path="clean.html"><a href="clean.html#两个数据集"><i class="fa fa-check"></i><b>4.3</b> 两个数据集</a></li>
<li class="chapter" data-level="4.4" data-path="clean.html"><a href="clean.html#多个数据集"><i class="fa fa-check"></i><b>4.4</b> 多个数据集</a></li>
<li class="chapter" data-level="4.5" data-path="clean.html"><a href="clean.html#常见问题与方案-3"><i class="fa fa-check"></i><b>4.5</b> 常见问题与方案</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="visualization.html"><a href="visualization.html"><i class="fa fa-check"></i><b>5</b> 数据可视化</a><ul>
<li class="chapter" data-level="" data-path="visualization.html"><a href="visualization.html#常见问题与方案-4"><i class="fa fa-check"></i>常见问题与方案</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="model.html"><a href="model.html"><i class="fa fa-check"></i><b>6</b> 统计建模</a><ul>
<li class="chapter" data-level="" data-path="model.html"><a href="model.html#常见问题与方案-5"><i class="fa fa-check"></i>常见问题与方案</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="report.html"><a href="report.html"><i class="fa fa-check"></i><b>7</b> 结果展示</a><ul>
<li class="chapter" data-level="7.1" data-path="report.html"><a href="report.html#图形"><i class="fa fa-check"></i><b>7.1</b> 图形</a></li>
<li class="chapter" data-level="7.2" data-path="report.html"><a href="report.html#表格"><i class="fa fa-check"></i><b>7.2</b> 表格</a><ul>
<li class="chapter" data-level="7.2.1" data-path="report.html"><a href="report.html#excel-1"><i class="fa fa-check"></i><b>7.2.1</b> Excel</a></li>
</ul></li>
<li class="chapter" data-level="7.3" data-path="report.html"><a href="report.html#rmarkdown"><i class="fa fa-check"></i><b>7.3</b> RMarkdown</a></li>
<li class="chapter" data-level="7.4" data-path="report.html"><a href="report.html#shiny"><i class="fa fa-check"></i><b>7.4</b> Shiny</a></li>
<li class="chapter" data-level="7.5" data-path="report.html"><a href="report.html#常见问题与方案-6"><i class="fa fa-check"></i><b>7.5</b> 常见问题与方案</a><ul>
<li class="chapter" data-level="7.5.1" data-path="report.html"><a href="report.html#export-包集才华与一身"><i class="fa fa-check"></i><b>7.5.1</b> export 包：集才华与一身</a></li>
</ul></li>
</ul></li>
<li class="chapter" data-level="8" data-path="bioapp.html"><a href="bioapp.html"><i class="fa fa-check"></i><b>8</b> 生物信息学应用</a></li>
<li class="appendix"><span><b>附录</b></span></li>
<li class="chapter" data-level="A" data-path="search-table.html"><a href="search-table.html"><i class="fa fa-check"></i><b>A</b> 速查表</a><ul>
<li class="chapter" data-level="A.1" data-path="search-table.html"><a href="search-table.html#数值计算函数"><i class="fa fa-check"></i><b>A.1</b> 数值计算函数</a></li>
<li class="chapter" data-level="A.2" data-path="search-table.html"><a href="search-table.html#模型符号与函数"><i class="fa fa-check"></i><b>A.2</b> 模型符号与函数</a></li>
<li class="chapter" data-level="A.3" data-path="search-table.html"><a href="search-table.html#统计分析与检验"><i class="fa fa-check"></i><b>A.3</b> 统计分析与检验</a></li>
</ul></li>
<li class="chapter" data-level="B" data-path="expand-reading.html"><a href="expand-reading.html"><i class="fa fa-check"></i><b>B</b> 拓展阅读</a><ul>
<li class="chapter" data-level="B.1" data-path="expand-reading.html"><a href="expand-reading.html#生信技能树语雀知识库"><i class="fa fa-check"></i><b>B.1</b> 生信技能树语雀知识库</a></li>
<li class="chapter" data-level="B.2" data-path="expand-reading.html"><a href="expand-reading.html#图书"><i class="fa fa-check"></i><b>B.2</b> 图书</a><ul>
<li class="chapter" data-level="B.2.1" data-path="expand-reading.html"><a href="expand-reading.html#问题与方案"><i class="fa fa-check"></i><b>B.2.1</b> 问题与方案</a></li>
<li class="chapter" data-level="B.2.2" data-path="expand-reading.html"><a href="expand-reading.html#统计建模"><i class="fa fa-check"></i><b>B.2.2</b> 统计建模</a></li>
<li class="chapter" data-level="B.2.3" data-path="expand-reading.html"><a href="expand-reading.html#核心集合"><i class="fa fa-check"></i><b>B.2.3</b> 核心集合</a></li>
<li class="chapter" data-level="B.2.4" data-path="expand-reading.html"><a href="expand-reading.html#生物信息学"><i class="fa fa-check"></i><b>B.2.4</b> 生物信息学</a></li>
<li class="chapter" data-level="B.2.5" data-path="expand-reading.html"><a href="expand-reading.html#r"><i class="fa fa-check"></i><b>B.2.5</b> R</a></li>
</ul></li>
<li class="chapter" data-level="B.3" data-path="expand-reading.html"><a href="expand-reading.html#视频"><i class="fa fa-check"></i><b>B.3</b> 视频</a></li>
<li class="chapter" data-level="B.4" data-path="expand-reading.html"><a href="expand-reading.html#公众号"><i class="fa fa-check"></i><b>B.4</b> 公众号</a></li>
<li class="chapter" data-level="B.5" data-path="expand-reading.html"><a href="expand-reading.html#其他资料"><i class="fa fa-check"></i><b>B.5</b> 其他资料</a></li>
</ul></li>
<li class="chapter" data-level="" data-path="references.html"><a href="references.html"><i class="fa fa-check"></i>参考文献</a></li>
</ul>

      </nav>
    </div>

    <div class="book-body">
      <div class="body-inner">
        <div class="book-header" role="navigation">
          <h1>
            <i class="fa fa-circle-o-notch fa-spin"></i><a href="./">极客R：数据分析之道</a>
          </h1>
        </div>

        <div class="page-wrapper" tabindex="-1" role="main">
          <div class="page-inner">

            <section class="normal" id="section-">
<div id="prepare" class="section level1">
<h1><span class="header-section-number">第 1 章</span> 准备工作</h1>
<p>想要在本地计算机上运行 R 语言代码，读者需要安装 R。为了方便 R 语言环境的使用和代码编写，我推荐读者使用 RStudio 公司开发的 R 集成开发环境（IDE）RStudio。</p>
<p>如果读者已经在计算机上安装好 R 和 RStudio，可以跳过本节内容。</p>
<p>如果读者倾向于使用像 VS Code 和 Idea 这样的流行编辑器，请自行安装对应的 R 语言拓展插件。</p>
<div id="r-的下载和安装" class="section level2">
<h2><span class="header-section-number">1.1</span> R 的下载和安装</h2>
<p>点击链接 <a href="https://mirrors.tuna.tsinghua.edu.cn/CRAN/" class="uri">https://mirrors.tuna.tsinghua.edu.cn/CRAN/</a> 进入 CRAN 的清华镜像源，选择与自己操作系统对应的 R 进行下载。</p>
<ul>
<li>Windows 系统用户进入 <a href="https://mirrors.tuna.tsinghua.edu.cn/CRAN/">base 子目录</a>，点击 <strong>Download R x.x.x for Windows</strong>。</li>
<li>MacOS 系统用户通过镜像地址进入 <strong>R for Mac OS X</strong> 页面后，点击 <code>R-x.x.x.pkg</code> 进行下载。</li>
<li>Linux 系统发行版众多，且需要一些额外的技术知识，请相应用户请阅读本章【<strong>常见问题与方案</strong>】一节学习 R 在 Linux 发行版下的安装。</li>
</ul>
<blockquote>
<p>上述的 <code>x.x.x</code> 指代 R 的版本号，目前是 <code>3.6.3</code>（2020-03），以用户下载时的实际版本号为准。</p>
</blockquote>
<p>对于 MacOS 系统用户，下载后直接双击一路向下进行傻瓜式操作即可安装成功。</p>
<p>对于 Windows 系统用户，根据笔者几年的安装和使用经验，安装时有一些额外的注意事项：</p>
<ol style="list-style-type: decimal">
<li>Windows 一般包含多个盘符，请读者尽量不要将 R 安装在 <code>Program files</code> 这样有空格的目录或中文目录下。读者可以自行创建一个专门的工具用于安装 R 以及放置 R 的三方包，如 <code>C:/Tools</code>，见图 <a href="prepare.html#fig:set-install-path">1.1</a>。</li>
<li>读者的电脑如果有 SSD 固态硬盘，最好将 R 安装到固态硬盘目录里，因为读写数据快。</li>
<li>一般现在电脑是 64 位的，如果你确定是这样，在安装时有关于 32 位的选项都可以不勾选，如图 <a href="prepare.html#fig:select-64bit">1.2</a>。</li>
<li>如果涉及到添加环境变量/路径之类的步骤，勾选添加即可。</li>
<li>以上没提到的，一路点下一步。</li>
</ol>
<div class="figure" style="text-align: center"><span id="fig:set-install-path"></span>
<img src="fig/ch01-set-install-path-win.PNG" alt="设置安装路径"  />
<p class="caption">
图 1.1: 设置安装路径
</p>
</div>
<div class="figure" style="text-align: center"><span id="fig:select-64bit"></span>
<img src="fig/ch01-select-64-bit.PNG" alt="只选择 64 位"  />
<p class="caption">
图 1.2: 只选择 64 位
</p>
</div>
<p>在 Windows 或 MacOS 下读者如果想要安装含有像 C++ 这样的源码的包，需要安装编译工具如 g++，这些编译工具都被 R 语言团队打包成了 Rtools，如果读者有这方面的需求，请阅读本章【<strong>常见问题与方案</strong>】一节学习 Rtools 的安装。</p>
</div>
<div id="rstudio-的下载和安装" class="section level2">
<h2><span class="header-section-number">1.2</span> RStudio 的下载和安装</h2>
<p>点击链接 <a href="https://rstudio.com/products/rstudio/download/" class="uri">https://rstudio.com/products/rstudio/download/</a> 进入 RStudio 下载界面，根据自己的操作系统选择适合的安装文件进行安装即可，如图 <a href="prepare.html#fig:install-rstudio">1.3</a>。</p>
<div class="figure" style="text-align: center"><span id="fig:install-rstudio"></span>
<img src="fig/ch01-download-rstudio.PNG" alt="下载 RStudio"  />
<p class="caption">
图 1.3: 下载 RStudio
</p>
</div>
<p>RStudio 的安装非常简单，没有特别的注意事项，读者可以直接一路点击向下。另外，读者可以自定义 RStudio 的安装路径，这不会影响 R 的使用。</p>
<p>RStudio 安装完成后可以像普通软件一样搜索和双击打开，如图 <a href="prepare.html#fig:rstudio-overview">1.4</a>。</p>
<div class="figure" style="text-align: center"><span id="fig:rstudio-overview"></span>
<img src="fig/ch01-rstudio-overview.PNG" alt="RStudio 界面"  />
<p class="caption">
图 1.4: RStudio 界面
</p>
</div>
<p>下面对 RStudio 的界面进行简单介绍，读者在后续的使用中将会逐渐熟悉。</p>
<ul>
<li>最上方是菜单栏，所有的功能都可以通过它们找到，没事可以多点一点。</li>
<li>左上方是代码编辑窗口，平时写代码的地方。</li>
<li>左下方是 R 控制台（还有终端和任务设定），我们可以通过控制台键入命令并观察 R 的输出（在代码编辑窗口中通过 Ctrl + Enter 快捷键可以将光标行代码发送到控制台，<strong>非常有用</strong>）。</li>
<li>整个右方是一些辅助窗口，最重要的是右上方的环境 Environment 窗口（用于展示当前环境中的对象：包括变量、函数等）和下方的 5 个窗口：Files、Plots、Packages、Help 以及 Viewer。</li>
</ul>
</div>
<div id="配置可选" class="section level2">
<h2><span class="header-section-number">1.3</span> 配置（可选）</h2>
<blockquote>
<p>下面的说明针对的是 Windows 系统，但对于其他系统也可以进行类似的设定。</p>
</blockquote>
<p>Windows 下的 R 默认使用用户文档目录作为家目录（等同于 Linux 中的<code>~</code>），使用系统指定的临时目录作为临时目录，使用安装路径下的 <code>R版本/library</code> 目录作为 R 包存储目录。</p>
<p>如果读者什么都选择默认的，</p>
<ul>
<li>当你一时安装包过多，或者装了电脑管家之类的管理软件时，系统的临时目录经常会把 RStudio 锁死，导致不能进行读写。</li>
<li>当你想要更新 R 版本时，有时你不得不面临重装所有包的举动（如果你使用几个月，装了几百个包…），或者想其他办法解决。</li>
</ul>
<p>下面介绍如何创建自定义的临时目录与包目录，这样上面情况都不会发生了。</p>
<p>读者在安装好 R 和 RStudio 后，打开 RStudio，在 R 控制台键入：</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="kw">file.edit</span>(<span class="st">&quot;~/.Rprofile&quot;</span>)</a></code></pre></div>
<p>在启动RStudio时，RStudio会首先执行里面的 R 代码，所以我们可以在这里用 R 代码进行配置。</p>
<p>首先在该文档内添加内容：</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb2-1" data-line-number="1"><span class="co">#--------------------------------------------</span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2"><span class="co"># Set custom library and temp directory for R</span></a>
<a class="sourceLine" id="cb2-3" data-line-number="3"><span class="co"># </span><span class="al">NOTE</span><span class="co">: please only change following 2 paths</span></a>
<a class="sourceLine" id="cb2-4" data-line-number="4"><span class="co">#   Any Question, please email to </span></a>
<a class="sourceLine" id="cb2-5" data-line-number="5"><span class="co">#       Shixiang Wang &lt;w_shixiang@163.com&gt;</span></a>
<a class="sourceLine" id="cb2-6" data-line-number="6"><span class="co">#--------------------------------------------</span></a>
<a class="sourceLine" id="cb2-7" data-line-number="7">.CUSTOM_LIB =<span class="st"> &quot;C:/Tools/R/R_Library&quot;</span> <span class="co"># set your custom library location</span></a>
<a class="sourceLine" id="cb2-8" data-line-number="8">.TMP =<span class="st"> &quot;C:/Tools/R/Rtmp&quot;</span>             <span class="co"># set a temp dir for R running</span></a>
<a class="sourceLine" id="cb2-9" data-line-number="9">                                     <span class="co"># please do not add &#39;/&#39; at the end !!!</span></a>
<a class="sourceLine" id="cb2-10" data-line-number="10"></a>
<a class="sourceLine" id="cb2-11" data-line-number="11"><span class="cf">if</span>(<span class="op">!</span><span class="kw">dir.exists</span>(.CUSTOM_LIB)){</a>
<a class="sourceLine" id="cb2-12" data-line-number="12">    <span class="kw">dir.create</span>(.CUSTOM_LIB, <span class="dt">recursive =</span> <span class="ot">TRUE</span>)</a>
<a class="sourceLine" id="cb2-13" data-line-number="13">}</a>
<a class="sourceLine" id="cb2-14" data-line-number="14"></a>
<a class="sourceLine" id="cb2-15" data-line-number="15"><span class="kw">.libPaths</span>(<span class="kw">c</span>(.CUSTOM_LIB, <span class="kw">.libPaths</span>()))</a>
<a class="sourceLine" id="cb2-16" data-line-number="16"><span class="kw">message</span>(<span class="st">&quot;Using library: &quot;</span>, <span class="kw">.libPaths</span>()[<span class="dv">1</span>])</a>
<a class="sourceLine" id="cb2-17" data-line-number="17"></a>
<a class="sourceLine" id="cb2-18" data-line-number="18"></a>
<a class="sourceLine" id="cb2-19" data-line-number="19"><span class="cf">if</span>(<span class="kw">dirname</span>(<span class="kw">tempdir</span>()) <span class="op">!=</span><span class="st"> </span>.TMP){</a>
<a class="sourceLine" id="cb2-20" data-line-number="20">    <span class="cf">if</span>(<span class="op">!</span><span class="kw">dir.exists</span>(.TMP)) <span class="kw">dir.create</span>(.TMP, <span class="dt">recursive =</span> <span class="ot">TRUE</span>)</a>
<a class="sourceLine" id="cb2-21" data-line-number="21">    <span class="kw">cat</span>(<span class="kw">paste0</span>(<span class="st">&quot;TMPDIR = &quot;</span>, .TMP), <span class="dt">file=</span><span class="st">&quot;~/.Renviron&quot;</span>, <span class="dt">sep =</span> <span class="st">&quot;</span><span class="ch">\n</span><span class="st">&quot;</span>)</a>
<a class="sourceLine" id="cb2-22" data-line-number="22">}</a>
<a class="sourceLine" id="cb2-23" data-line-number="23"><span class="kw">message</span>(<span class="st">&quot;Using temp directory: &quot;</span>, .TMP)</a>
<a class="sourceLine" id="cb2-24" data-line-number="24"></a>
<a class="sourceLine" id="cb2-25" data-line-number="25"><span class="co">#---------------------------------------------------</span></a>
<a class="sourceLine" id="cb2-26" data-line-number="26"><span class="co"># pacman is optional, you can delete following code</span></a>
<a class="sourceLine" id="cb2-27" data-line-number="27"><span class="co"># If you wanna use pacman, please read:</span></a>
<a class="sourceLine" id="cb2-28" data-line-number="28"><span class="co">#   &lt;https://www.jianshu.com/p/cb16ded75672&gt;</span></a>
<a class="sourceLine" id="cb2-29" data-line-number="29"><span class="co"># Basically, </span></a>
<a class="sourceLine" id="cb2-30" data-line-number="30"><span class="co"># #1, you can use &#39;p_load&#39; to load multiple package into R</span></a>
<a class="sourceLine" id="cb2-31" data-line-number="31"><span class="co">#       like p_load(data.table, dplyr)</span></a>
<a class="sourceLine" id="cb2-32" data-line-number="32"><span class="co"># #2, you can use &#39;p_get&#39; just to install package</span></a>
<a class="sourceLine" id="cb2-33" data-line-number="33"><span class="co"># #3, you can use &#39;p_update&#39; to update all packages</span></a>
<a class="sourceLine" id="cb2-34" data-line-number="34"><span class="co">#---------------------------------------------------</span></a>
<a class="sourceLine" id="cb2-35" data-line-number="35"><span class="cf">if</span>(<span class="op">!</span><span class="kw">require</span>(pacman)){</a>
<a class="sourceLine" id="cb2-36" data-line-number="36">    <span class="kw">install.packages</span>(<span class="st">&quot;pacman&quot;</span>, <span class="dt">dependencies =</span> <span class="ot">TRUE</span>)</a>
<a class="sourceLine" id="cb2-37" data-line-number="37">}</a>
<a class="sourceLine" id="cb2-38" data-line-number="38"><span class="kw">library</span>(pacman)</a>
<a class="sourceLine" id="cb2-39" data-line-number="39"><span class="co">#----------------------------------------------------</span></a></code></pre></div>
<p>然后根据情况对上述内容中的目录设定进行修改即可。</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb3-1" data-line-number="1">.CUSTOM_LIB =<span class="st"> &quot;C:/Tools/R/R_Library&quot;</span> <span class="co"># set your custom library location</span></a>
<a class="sourceLine" id="cb3-2" data-line-number="2">.TMP =<span class="st"> &quot;C:/Tools/R/Rtmp&quot;</span>             <span class="co"># set a temp dir for R running</span></a>
<a class="sourceLine" id="cb3-3" data-line-number="3">                                     <span class="co"># please do not add &#39;/&#39; at the end !!!</span></a></code></pre></div>
<p><strong>pacman</strong> 那段代码是可选的，该包是 <code>library()</code> 函数的替代品，使用它安装和管理 R 包更简单。具体的使用方法可以<a href="https://www.jianshu.com/p/cb16ded75672">点击阅读</a>我之前的简书文章。</p>
<p>这里为了方便大家使用，我添加了一些必要注释，如果上述配置存在问题，读者可以发邮件给我。</p>
<p>保存后重启 RStudio 或者点击菜单栏 <strong>Session</strong> 下的 <strong>Restart R</strong>。</p>
<p>以后 RStudio 每次启动后都会输出读者计算机中 R 包的存储路径和它使用的临时路径。读者如果以后升级 R，只要重装下 R 安装文件就可以了，R 包的目录并不会改动，键入下面的命令可以更新所有的 R 包：</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb4-1" data-line-number="1"><span class="kw">p_update</span>()</a></code></pre></div>
</div>
<div id="常见问题与方案" class="section level2">
<h2><span class="header-section-number">1.4</span> 常见问题与方案</h2>
<p>除了本节目前罗列的问题，读者在学习本章内容时遇到的其他问题都可以通过 <a href="https://github.com/ShixiangWang/geek-r-tutorial/issues">GitHub Issue</a> 提出和进行讨论。如果读者提出的是通性问题，将增补到该节。</p>
<div id="r-在-linux-系统下的安装" class="section level3">
<h3><span class="header-section-number">1.4.1</span> R 在 Linux 系统下的安装</h3>
<div id="ubuntu-从源安装-r" class="section level4">
<h4><span class="header-section-number">1.4.1.1</span> Ubuntu 从源安装 R</h4>
<p>下述操作基于在 Ubuntu 18 系统上安装 R 3.5 的实践。</p>
<blockquote>
<p><code>$</code> 前缀指明命令在 <code>shell</code> 终端中进行。</p>
</blockquote>
<p>安装依赖：</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb5-1" data-line-number="1">$ <span class="fu">sudo</span> apt-get install xorg-dev libreadline-dev</a>
<a class="sourceLine" id="cb5-2" data-line-number="2">$ <span class="fu">sudo</span> apt-get install libcurl4-openssl-dev</a>
<a class="sourceLine" id="cb5-3" data-line-number="3">$ <span class="fu">sudo</span> apt-get install libbz2-dev</a>
<a class="sourceLine" id="cb5-4" data-line-number="4">$ <span class="fu">sudo</span> apt-get install libcairo2-dev libgtk2.0-dev</a>
<a class="sourceLine" id="cb5-5" data-line-number="5">$ <span class="fu">sudo</span> apt-get install texinfo texlive</a>
<a class="sourceLine" id="cb5-6" data-line-number="6">$ <span class="fu">wget</span> http://mirrors.ctan.org/fonts/inconsolata.zip</a>
<a class="sourceLine" id="cb5-7" data-line-number="7">$ <span class="fu">sudo</span> cp -Rfp inconsolata/* /usr/share/texmf/</a>
<a class="sourceLine" id="cb5-8" data-line-number="8">$ <span class="co"># 或者 sudo cp -r inconsolata/ /usr/share/texlive/texmf-dist/tex/latex/</span></a>
<a class="sourceLine" id="cb5-9" data-line-number="9">$ <span class="fu">sudo</span> mktexlsr <span class="co"># 刷新</span></a>
<a class="sourceLine" id="cb5-10" data-line-number="10"></a>
<a class="sourceLine" id="cb5-11" data-line-number="11"><span class="co"># 如果没有java解释器，安装下</span></a>
<a class="sourceLine" id="cb5-12" data-line-number="12">$ <span class="fu">sudo</span> apt-get install default-jdk</a></code></pre></div>
<p>下载与安装 R：</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb6-1" data-line-number="1">$ <span class="ex">curl</span> -O http://cran.utstat.utoronto.ca/src/base/R-3/R-3.5.0.tar.gz</a>
<a class="sourceLine" id="cb6-2" data-line-number="2">$ <span class="fu">tar</span> -zxvf R-3.5.0.tar.gz</a>
<a class="sourceLine" id="cb6-3" data-line-number="3">$ <span class="bu">cd</span> R-3.5.0/</a>
<a class="sourceLine" id="cb6-4" data-line-number="4">$  <span class="ex">./configure</span> --prefix=<span class="va">$HOME</span>/local/R --enable-R-shlib --with-cairo=yes</a>
<a class="sourceLine" id="cb6-5" data-line-number="5">$ <span class="co"># ./configure --with-cairo --with-libpng --with-libtiff --with-jpeglib --enable-R-shlib --prefix=$HOME/local/R</span></a>
<a class="sourceLine" id="cb6-6" data-line-number="6">$ <span class="fu">make</span></a>
<a class="sourceLine" id="cb6-7" data-line-number="7">$ <span class="fu">make</span> install</a></code></pre></div>
<p>创建符号链接：</p>
<div class="sourceCode" id="cb7"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb7-1" data-line-number="1">$ <span class="bu">cd</span> /usr/bin/</a>
<a class="sourceLine" id="cb7-2" data-line-number="2">$ <span class="fu">sudo</span> ln -s <span class="va">$HOME</span>/local/R/bin/Rscript Rscript</a>
<a class="sourceLine" id="cb7-3" data-line-number="3">$ <span class="fu">sudo</span> ln -s <span class="va">$HOME</span>/local/R/bin/R R</a></code></pre></div>
<p>检查：</p>
<div class="sourceCode" id="cb8"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb8-1" data-line-number="1">$ <span class="fu">ls</span> -l R*</a>
<a class="sourceLine" id="cb8-2" data-line-number="2"><span class="ex">lrwxrwxrwx</span> 1 root root 23 6月  28 17:01 R -<span class="op">&gt;</span> /home/wsx/local/R/bin/R</a>
<a class="sourceLine" id="cb8-3" data-line-number="3"><span class="ex">lrwxrwxrwx</span> 1 root root 29 6月  28 17:01 Rscript -<span class="op">&gt;</span> /home/wsx/local/R/bin/Rscript</a></code></pre></div>
<p>使用：</p>
<div class="sourceCode" id="cb9"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb9-1" data-line-number="1">$ <span class="ex">R</span></a>
<a class="sourceLine" id="cb9-2" data-line-number="2"></a>
<a class="sourceLine" id="cb9-3" data-line-number="3"><span class="ex">R</span> version 3.5.0 (2018-04-23) <span class="ex">--</span> <span class="st">&quot;Joy in Playing&quot;</span></a>
<a class="sourceLine" id="cb9-4" data-line-number="4"><span class="ex">Copyright</span> (C) <span class="ex">2018</span> The R Foundation for Statistical Computing</a>
<a class="sourceLine" id="cb9-5" data-line-number="5"><span class="ex">Platform</span>: x86_64-pc-linux-gnu (64-bit)</a>
<a class="sourceLine" id="cb9-6" data-line-number="6"></a>
<a class="sourceLine" id="cb9-7" data-line-number="7"><span class="ex">R</span> is free software and comes with ABSOLUTELY NO WARRANTY.</a>
<a class="sourceLine" id="cb9-8" data-line-number="8"><span class="ex">You</span> are welcome to redistribute it under certain conditions.</a>
<a class="sourceLine" id="cb9-9" data-line-number="9"><span class="ex">Type</span> <span class="st">&#39;license()&#39;</span> or <span class="st">&#39;licence()&#39;</span> for distribution details.</a>
<a class="sourceLine" id="cb9-10" data-line-number="10"></a>
<a class="sourceLine" id="cb9-11" data-line-number="11">  <span class="ex">Natural</span> language support but running in an English locale</a>
<a class="sourceLine" id="cb9-12" data-line-number="12"></a>
<a class="sourceLine" id="cb9-13" data-line-number="13"><span class="ex">R</span> is a collaborative project with many contributors.</a>
<a class="sourceLine" id="cb9-14" data-line-number="14"><span class="ex">Type</span> <span class="st">&#39;contributors()&#39;</span> for more information and</a>
<a class="sourceLine" id="cb9-15" data-line-number="15"><span class="st">&#39;citation()&#39;</span> <span class="ex">on</span> how to cite R or R packages in publications.</a>
<a class="sourceLine" id="cb9-16" data-line-number="16"></a>
<a class="sourceLine" id="cb9-17" data-line-number="17"><span class="ex">Type</span> <span class="st">&#39;demo()&#39;</span> for some demos, <span class="st">&#39;help()&#39;</span> for on-line help, or</a>
<a class="sourceLine" id="cb9-18" data-line-number="18"><span class="st">&#39;help.start()&#39;</span> <span class="kw">for</span> <span class="ex">an</span> HTML browser interface to help.</a>
<a class="sourceLine" id="cb9-19" data-line-number="19"><span class="ex">Type</span> <span class="st">&#39;q()&#39;</span> to quit R.</a>
<a class="sourceLine" id="cb9-20" data-line-number="20"></a>
<a class="sourceLine" id="cb9-21" data-line-number="21"><span class="op">&gt;</span> </a></code></pre></div>
<p>如果遇到编译问题和报错，读者可以参考下面两篇博文：</p>
<ul>
<li><a href="https://www.cnblogs.com/wing1995/p/4635371.html">ubuntu 上安装 R 的时候遇到的问题总结</a></li>
<li><a href="http://m.blog.itpub.net/9036/viewspace-2122686/">CentOS 下 R 的安装</a></li>
</ul>
</div>
<div id="centos-从源安装-r" class="section level4">
<h4><span class="header-section-number">1.4.1.2</span> CentOS 从源安装 R</h4>
<p>下述操作基于在 CentOS 系统上安装 R 3.6 的实践。</p>
<blockquote>
<p><code>$</code> 前缀指明命令在 <code>shell</code> 终端中进行。</p>
</blockquote>
<p>首先切入 root 账户。</p>
<p>安装 R 编译依赖：</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb10-1" data-line-number="1">$ <span class="ex">yum-builddep</span> R</a></code></pre></div>
<p>消灭界面configure警告：<code>configure: WARNING: neither inconsolata.sty nor zi4.sty found: PDF vignettes and package manuals will not be rendered optimally</code></p>
<div class="sourceCode" id="cb11"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb11-1" data-line-number="1">$ <span class="fu">wget</span> http://mirrors.ctan.org/fonts/inconsolata.zip</a>
<a class="sourceLine" id="cb11-2" data-line-number="2">$ <span class="ex">Unzip</span> inconsolata.zip </a>
<a class="sourceLine" id="cb11-3" data-line-number="3">$ <span class="fu">cp</span> -Rfp inconsolata/* /usr/share/texmf/</a>
<a class="sourceLine" id="cb11-4" data-line-number="4">$ <span class="ex">mktexlsr</span></a></code></pre></div>
<p>下载 R 并解压：</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb12-1" data-line-number="1">$ <span class="fu">wget</span> -c https://cran.r-project.org/src/base/R-3/R-3.6.0.tar.gz</a>
<a class="sourceLine" id="cb12-2" data-line-number="2">$ <span class="fu">tar</span> zxvf R-3.6.0.tar.gz </a></code></pre></div>
<p>编译安装</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb13-1" data-line-number="1">$ <span class="bu">cd</span> R-3.6.0/</a>
<a class="sourceLine" id="cb13-2" data-line-number="2">$ <span class="ex">./configure</span> --prefix=/home/public/R/R-base --enable-R-shlib --with-cairo=yes</a></code></pre></div>
<p>这里 <code>prefix</code> 后面的路径可以自己指定。</p>
<p>然后</p>
<div class="sourceCode" id="cb14"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb14-1" data-line-number="1">$ <span class="fu">make</span></a>
<a class="sourceLine" id="cb14-2" data-line-number="2">$ <span class="fu">make</span> install</a></code></pre></div>
<p>最后可以把它链接到常用软件位置上去。</p>
<div class="sourceCode" id="cb15"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb15-1" data-line-number="1">$ <span class="fu">sudo</span> ln -s /home/public/R/R-basebin/R /bin/R </a></code></pre></div>
</div>
</div>
<div id="rtools-安装" class="section level3">
<h3><span class="header-section-number">1.4.2</span> Rtools 安装</h3>
<div id="windows-系统-rtools" class="section level4">
<h4><span class="header-section-number">1.4.2.1</span> Windows 系统 Rtools</h4>
<p>点击链接 <a href="https://mirrors.tuna.tsinghua.edu.cn/CRAN/" class="uri">https://mirrors.tuna.tsinghua.edu.cn/CRAN/</a> 进入 CRAN 页面，依次点击【Download R for Windows】、【Rtools】，然后选择推荐的版本下载和安装，如图 <a href="prepare.html#fig:rtools-win">1.5</a>。</p>
<div class="figure" style="text-align: center"><span id="fig:rtools-win"></span>
<img src="fig/ch01-rtools-win.PNG" alt="Windows 下 Rtools 的下载"  />
<p class="caption">
图 1.5: Windows 下 Rtools 的下载
</p>
</div>
<p>安装时一路点击向下，建议不要修改安装路径，因为 R 的很多编译环境变量设定都指向默认的安装路径，读者如果修改它后续很可能引起不必要的麻烦。</p>
</div>
<div id="macos-系统-rtools" class="section level4">
<h4><span class="header-section-number">1.4.2.2</span> MacOS 系统 Rtools</h4>
<p>点击链接 <a href="https://mirrors.tuna.tsinghua.edu.cn/CRAN/" class="uri">https://mirrors.tuna.tsinghua.edu.cn/CRAN/</a> 进入 CRAN 页面，依次点击【Download R for Mac OS X】、【tools】，然后选择最新版的 <code>clang-x.x.x.pkg</code> 和 <code>gfortran-x.x.x.pkg</code> 下载和安装即可，如图 <a href="prepare.html#fig:rtools-mac">1.6</a>。</p>
<blockquote>
<p><code>x.x.x</code> 指代版本号，如 <code>8.0.0</code>。</p>
</blockquote>
<div class="figure" style="text-align: center"><span id="fig:rtools-mac"></span>
<img src="fig/ch01-rtools-mac.PNG" alt="MacOS 下 Rtools 的下载"  />
<p class="caption">
图 1.6: MacOS 下 Rtools 的下载
</p>
</div>
</div>
</div>
<div id="rstudio-server-安装" class="section level3">
<h3><span class="header-section-number">1.4.3</span> RStudio Server 安装</h3>
<p>RStudio Server 比较好用，适用于安装在小型服务器上以方便多人同时利用服务器的计算资源。需要注意，RStudio Server 的安装需要管理员（root）权限。</p>
<p>点击链接 <a href="https://rstudio.com/products/rstudio/download-server/" class="uri">https://rstudio.com/products/rstudio/download-server/</a> 进入 RStudio Server 下载页面，针对不同的 Linux 发行版，RStudio 公司已经有良好的文档支持，所以我不再赘述。</p>
<p>在使用 RStudio Server 时我有一个小小的建议，如我在前面的配置一节所说，读者可以自己创建一个专门的目录管理所有的 R 包。并且针对该目录，建议新建一个 Linux 群组以管理它的访问权限。</p>
<p>这里简介一下安装后添加其他人，以增加用户 rotation 为例：</p>
<div class="sourceCode" id="cb16"><pre class="sourceCode sh"><code class="sourceCode bash"><a class="sourceLine" id="cb16-1" data-line-number="1">$ <span class="fu">sudo</span> useradd rotation -d /home/rotation -m -g wsx   # 将新增用户添加到你所在群组</a>
<a class="sourceLine" id="cb16-2" data-line-number="2">$ <span class="fu">sudo</span> passwd rotation                                # 设定初始密码 </a>
<a class="sourceLine" id="cb16-3" data-line-number="3">$ <span class="fu">sudo</span> adduser rotation sudo                          # 给用户添加sudo权限，以方便一些操作</a></code></pre></div>

</div>
</div>
</div>
            </section>

          </div>
        </div>
      </div>
<a href="author.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="base.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
    </div>
  </div>
<script src="libs/gitbook/js/app.min.js"></script>
<script src="libs/gitbook/js/lunr.js"></script>
<script src="libs/gitbook/js/clipboard.min.js"></script>
<script src="libs/gitbook/js/plugin-search.js"></script>
<script src="libs/gitbook/js/plugin-sharing.js"></script>
<script src="libs/gitbook/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook/js/plugin-bookdown.js"></script>
<script src="libs/gitbook/js/jquery.highlight.js"></script>
<script src="libs/gitbook/js/plugin-clipboard.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": true,
"facebook": true,
"twitter": true,
"linkedin": false,
"weibo": false,
"instapaper": false,
"vk": false,
"all": ["facebook", "twitter", "linkedin", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/ShixiangWang/geek-r-tutorial/edit/master/01-prepare.Rmd",
"text": "编辑"
},
"history": {
"link": null,
"text": null
},
"view": {
"link": null,
"text": null
},
"download": null,
"toc": {
"collapse": "section"
}
});
});
</script>

<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    var src = "true";
    if (src === "" || src === "true") src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-MML-AM_CHTML";
    if (location.protocol !== "file:")
      if (/^https?:/.test(src))
        src = src.replace(/^https?:/, '');
    script.src = src;
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>
</body>

</html>
